package com.cn.longdistancebusstation.customView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.List;

/**
 * Created by mal on 2018/2/1.
 */

public class IndexSideBar extends View {
    private List<String> mIndexList;
    private Paint mPaint = new Paint();
    private int mChooseIndex = -1;
    private int mFontSize = 40;
    private OnLetterTouchedChangeListener mOnLetterTouchedChangeListener;
    //
     /*字母画笔*/
    private Paint wordsPaint;
    /*字母背景画笔*/
    private Paint bgPaint;
    /*每一个字母的宽度*/
    private int itemWidth;
    /*每一个字母的高度*/
    private int itemHeight;
    /*手指按下的字母索引*/
    private int touchIndex = 0;
    //

    public IndexSideBar(Context context) {
        super(context);
    }

    public IndexSideBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public IndexSideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 获得控件高度
        int viewHeight = getHeight();
        // 获得控件宽度
        int viewWidth = getWidth();
        if (mIndexList == null || mIndexList.size() == 0) return;
        // 控件高度除以索引字母个数得到每个索引字母的高度
        int heightPerAlphabet = viewHeight / (mIndexList.size()+1);

        //通过循环每个索引字母，并绘制出来
        for (int i = 0; i < mIndexList.size(); i++) {

            // 设置画笔颜色、画笔绘制文字粗细和大小，设置抗锯齿
            mPaint.setColor(Color.BLACK);
            mPaint.setTypeface(Typeface.DEFAULT);
            mPaint.setTextSize(mFontSize);
            mPaint.setAntiAlias(true);

            // 如果当前选择的索引字母下标和循环到的索引字母下标相同
            if (mChooseIndex == i) {

                // 设置画笔颜色，绘制文字大小和加粗
                mPaint.setColor(Color.BLUE);
                mPaint.setTextSize(mFontSize);
                mPaint.setFakeBoldText(true);
            }

            // 索引字母的相对于控件的x坐标，此处算法结果为居中
            float xPos = viewWidth / 2 - mPaint.measureText(mIndexList.get(i)) / 2;
            // 索引字母的相对于控件的y坐标，索引字母的高度乘以索引字母下标+1即为y坐标
            float yPos = heightPerAlphabet * i + heightPerAlphabet ;
            // 绘制索引字母
            canvas.drawText(mIndexList.get(i), xPos, yPos, mPaint);
            // 重置画笔，为绘制下一个索引字母做准备
            mPaint.reset();

        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // 获得触摸后的动作
        int action = event.getAction();
        // 获得触摸点的Y轴坐标
        float touchYPos=event.getY();

        // 控件高度除以索引字母的个数得到每个索引字母的高度（这里进行int强转），触摸点的Y轴坐标除以每个索引字母的高度就得到触摸到的索引字母的下标
        int currentTouchIndex= (int) (touchYPos/(getHeight())*(mIndexList.size()+1));
        Log.d("IndexSideBar", getHeight()+"");
        Log.d("IndexSideBar", currentTouchIndex+","+touchYPos);
        currentTouchIndex = currentTouchIndex>mIndexList.size()-1 ? mIndexList.size()-1 : currentTouchIndex;
        Log.d("IndexSideBar", currentTouchIndex+"");
        switch (action){
            // 当触摸的动作为按下或者按下移动时
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                // 设置背景颜色
                setBackgroundColor(Color.rgb(241,241,241));

                // 设置当前选的索引字母的下标值为当前选择的值
                mChooseIndex=currentTouchIndex;

                // 如果接口存在和索引下标值合法，执行接口方法，传入当前触摸的索引字母，供外部调用接收
                if(mOnLetterTouchedChangeListener!=null&&currentTouchIndex<mIndexList.size()&&currentTouchIndex>-1){
                    mOnLetterTouchedChangeListener.onTouchedLetterChange(mIndexList.get(mChooseIndex));
                }
                // 重新绘制控件，即重新执行onDraw函数
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.rgb(241,241,241));
                // 当停止触摸控件的时候，将当前选择的索引字母下标值设为-1
                mChooseIndex=-1;
                invalidate();
                break;
            default:break;

        }

        // 返回true表明该触摸事件处理完毕不分发出去
        return true;
    }

    public List<String> getIndexList() {
        return mIndexList;
    }

    public void setIndexList(List<String> indexList) {
        mIndexList = indexList;
        invalidate();
    }

    public void setOnLetterTouchedChangeListener(OnLetterTouchedChangeListener onLetterTouchedChangeListener) {
        mOnLetterTouchedChangeListener = onLetterTouchedChangeListener;
    }

    public interface OnLetterTouchedChangeListener{

        void onTouchedLetterChange(String letterTouched);

    }
}
